﻿<!DOCTYPE HTML>
<html>
<head>
    {include file="common:meta"}
</head>
<body>
<div class="page-container">
    <div class=" float-e-margins">
        <div class="ibox-content">
            <div class="tabs-container">
                <ul class="nav nav-tabs">
                    <li class="active"><a data-toggle="tab" href="#tab-1" aria-expanded="true">奖品列表</a>
                    </li>
                    <li class=""><a data-toggle="tab" href="#tab-2" aria-expanded="false">奖品添加</a>
                    </li>
                </ul>
                <div class="tab-content">
                    <div class=" tab-pane active" id="tab-1">
                        <table id="tb_departments"></table>
                    </div>
                    <div class=" tab-pane" id="tab-2">
                        <div class="ibox-content">
                            <form class="form-horizontal" id="form" data-url="/GoldEggNewPrizeAdd">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="gold_egg_id">
                                        <span class="c-red">*</span>适用宝箱类型：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <select class="form-control" name="gold_egg_id" id="gold_egg_id">
                                            {volist name='gold_egg_list' id='vo'}
                                            <option value="{$vo.id}">{$vo.egg_name}</option>
                                            {/volist}
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="gift_id">
                                        <span class="c-red">*</span>奖品名：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <select class="form-control" name="gift_id" id="gift_id">
                                            {volist name='gift_list' id='vo'}
                                            <option value="{$vo.id}">{$vo.gift_name}--{$vo.diamonds}钻</option>
                                            {/volist}
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="default_stock">
                                        <span class="c-red">*</span>默认库存：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <input type="number" class="form-control" id="default_stock"
                                               name="default_stock">
                                    </div>
                                </div>

                                <div class="form-group">
                                    <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                                        <button class="btn btn-primary" type="button" onclick="commonSubmit(this)">添加
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="common:footer"}
<script>
    $(document).ready(function () {
        initTable();
    });

    function initTable() {
        $('#tb_departments').bootstrapTable({
            url: '/GoldEggNewPrizeIndexData',         //请求后台的URL（*）
            dataField: "data",
            method: 'post',                      //请求方式（*）
            striped: true,                      //是否显示行间隔色
            cache: true,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            queryParams: queryParams,//传递参数（*）
            showToggle: true,
            showRefresh: true,
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                       //初始化加载第一页，默认第一页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
            showColumns: true,                  //是否显示所有的列
            showPaginationSwitch: true,
            minimumCountColumns: 2,             //最少允许的列数

            columns: [{
                field: 'id',
                title: 'ID',
                sortable: true
            }, {
                field: 'gift_name',
                title: '奖品名',
            }, {
                field: 'diamonds',
                title: '单价(钻)',
            }, {
                field: 'prize_stock',
                title: '当前库存',
                editable: {
                    type: 'text',
                    title: '当前库存'
                }
            }, {
                field: 'default_stock',
                title: '默认库存',
                editable: {
                    type: 'text',
                    title: '默认库存',
                    validate: function (v) {
                        if (!v) return '不能为空';
                    }
                }
            }, {
                field: 'gift_notice',
                title: '提示通知',
                editable: {
                    type: 'text',
                    title: '提示通知'
                }
            }, {
                field: 'egg_name',
                title: '适用宝箱类型',
                sortable: true
            }, {
                field: 'update_time',
                title: '更新时间',
            }, {
                field: 'Button',
                title: '操作',
                events: operateEvents,
                formatter: operateFormatter
            }],
            //保存的使用
            onEditableSave: function (field, row, oldValue, $el) {
                //可进行异步操作
                $.ajax({
                    type: "post",
                    url: "/GoldEggNewPrizeEdit",
                    data: row,
                    dataType: 'JSON',
                    success: function (re) {
                        if (re.code === 1) {
                            layer.msg(re.msg, {icon: 1, time: 2000});
                        } else {
                            layer.alert(re.msg);
                        }
                    },
                    error: function () {
                        alert('编辑失败');
                    },
                    complete: function () {

                    }

                });
            }
        });
    }

    //得到查询的参数

    function queryParams(params) {
        params.offset = params.offset / params.limit + 1;
        console.log(JSON.stringify(params));
        return params;
    }

    function operateFormatter(value, row, index) {
        var prize_status = row.status === 1 ? '下架' : '上架';
        return [
            '<button type="button"  class="TableChange btn btn-primary btn-sm" style="margin-right:15px;">' + prize_status + '</button>'
        ].join('');
    }

    window.operateEvents = {
        'click .TableChange': function (e, value, row, index) {
            changeStatus('GoldEggNewPrizeChangePrizeStatus', row, this);
        }
    };

    function changeStatus(url, row, obj) {
        var prize_status = row.status === 1 ? '上架' : '下架';
        row.status === 1 ? row.status = 0 : row.status = 1;
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: url,
            data: {'id': row.id},
            success: function (re) {
                if (re.code === 1) {
                    layer.msg(re.msg, {icon: 1, time: 2000});
                    $(obj).html(prize_status);
                } else {
                    layer.alert(re.msg);
                }
            },
            error: function (data) {
                console.log(data.msg);
            }
        });
    }
    // 搜索按钮触发事件
    $(function () {
        $("#eventQuery").click(function () {
            $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
        });

    });
</script>
</body>
</html>